<?php

namespace RedMVC;

/**
 * Layout class
 *  
 * @category    RedMVC
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class Layout extends View{

    protected $_title;
    protected $_head = '';
    protected $_content;
    protected $_errors = array();
    protected $_messages = array();
    
    public function setTitle($title){
        $translate = Registry::get('RedMVC_Translate');
        $this->_title = $translate->_($title);
    }
    
    public function getTitle(){
        return $this->_title;
    }
    
    public function addToHead($head){
        $this->_head .= $head."\n";
    }
    
    public function getHead(){
        return $this->_head;
    }
    
    public function setContent($content){
        $this->_content = $content;
    }
    
    public function getContent(){
        return $this->_content;
    }
    
    public function setErrors(array $errors){
        $this->_errors = $errors;
    }
    
    public function mergeErrors(array $errors){
        $this->_errors = array_merge($this->_errors, $errors);
    }
    
    public function addError($error){
        $translate = Registry::get('RedMVC_Translate');
        $this->_errors[] = $translate->_($error);
    }
    
    public function printErrors($template = null){
        if(count($this->_errors) == 0){
            return '';
        }
        $errors = implode('<br>', $this->_errors);
        if(null === $template){
            return $errors;
        }
        return sprintf($template, $errors);
    }
    
    public function setMessages(array $msgs){
        $this->_messages = $msgs;
    }
    
    public function mergeMessages(array $msgs){
        $this->_messages = array_merge($this->_messages, $msgs);
    }
    
    public function addMessage($msg){
        $translate = Registry::get('RedMVC_Translate');
        $this->_messages[] = $translate->_($msg);
    }
    
    public function printMessages($template = null){
        if(count($this->_messages) == 0){
            return '';
        }
        $messages = implode('<br>', $this->_messages);
        if(null === $template){
            return $messages;
        }
        return sprintf($template, $messages);
    }

}